<html>
	<head>
		<meta charset="utf-8" />
		<title>24</title>
		<style>
			.ans {
				display: block;
				width: 500px;
				/* background:black;
				cursor: pointer; */
			}

		</style>
	</head>
	<body>
		<script>
		
			var canFac = false;
			var canPow = false;
		
			Math.randRange = function(m, n) {
				return Math.floor(Math.random() * (n - m + 1) + m);
			};
			
			
			var nums = [34,9,3,8];
			
			for (var i = 0; i < 4; i ++) {
				nums[i] = Math.randRange(1, 9);	
			};
			document.write(nums.join(' ') + '<br />');
			document.write('<hr />');			
			var exprs = [];
			
			var printExpr = function() {
				for (var i = 0; i < exprs.length; i ++) {
					exprs[i] = replaceExpr(exprs[i]);
				} 
				document.write('<div class="ans" onclick="whiteMe(this)">' + exprs.join('<br/>') + '</div>');
			};
			
			var whiteMe = function(dom) {
				dom.style.backgroundColor = "white";
			}
			
			var newArr = function(arr, i, j) {
				var tmpArr = [0];
				for (var i1 = 0, n = arr.length; i1 < n; i1 ++) {
					if (i1 !== i && i1 !== j) {
						tmpArr.push(arr[i1]);
					}
				}
				return tmpArr;
			};
			
			var fac = function(n) {
				if (n < 10 && Math.abs(n - Math.round(n)) < 0.0001) {
					if (n == 0) return 1;
					var result = 1;
					for (var i = 1; i <= n; i ++ ) {
						result *= i;
					}
					return result;
				} else {
					return Infinity;
				}
			};
			
			var replaceExpr = function(expr) {
				
				var replace = function(n) {
					if (isNaN(n)) {
						return n;
					}
					if (n.indexOf('.') === -1) {
						return n;
					}
					n = parseFloat(n, 10);
					for (var i = 1; i <= 9; i ++) {
						for (var j = 2; j <= 30; j ++) {
							if (Math.abs(i / j - n) < 0.0001) {
								return '<span style="color: red">' + i + '/' + j + '</span>';
							}
						}
					}
					return n;
				};
				
				var arr = [];
				expr = expr.replace(/\*/g, '×').replace(/\//g, '÷');
				var symbols = ['+', '-', '×', '÷', '!', '^', '='];
				var last = 0;
				for (var i = 0, n = expr.length; i < n; i ++) {
					if (symbols.indexOf(expr[i]) != -1) {
						arr.push(expr.slice(last, i));
						arr.push(expr[i]);
						last = i + 1;						
					} 
				}
				arr.push(expr.slice(last));
				for (var i = 0, n = arr.length; i < n; i ++) {
					arr[i] = replace(arr[i]);
				}
				return arr.join(' ').replace(/\s\!/g, '!').replace(/\s\^\s/g, '^');
			};
			
			var search = function(arr) {
				if (arr[0] < 1) {
					return false;
				}
				var n = arr.length;
				if (n === 1) {
					if (Math.abs(arr[0] - 24) < 0.0001) {
						printExpr();
						return true;
					} else {
						return false;
					}
				}
				
				var exprs_n = exprs.length;
				for (var i = 0; i < n - 1; i ++) {
					for (var j = i + 1; j < n; j ++) {
						
						var arr1 = newArr(arr, i, j);
						var vi = arr[i];
						var vj = arr[j];
						/** 普通 */
						arr1[0] = vi + vj;
						exprs[exprs_n] = vi  + "+" + vj + "=" + arr1[0];
						if (search(arr1)) {
							return true;
						}
						
						arr1[0] = vi - arr[j];
						exprs[exprs_n] = vi  + "-" + vj + "=" + arr1[0];
						if (search(arr1)) {
							return true;
						}
						

						arr1[0] = vj - vi;
						exprs[exprs_n] = vj  + "-" + vi + "=" + arr1[0];
						if (search(arr1)) {
							return true;
						}
						
						arr1[0] = vi * vj;
						exprs[exprs_n] = vi  + "*" + vj + "=" + arr1[0];
						if (search(arr1)) {
							return true;
						}
						
						if (vj !== 0) {
							arr1[0] = vi / vj;
							exprs[exprs_n] = vi  + "/" + vj + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
						}
						
						if (vi !== 0) {
							arr1[0] = vj / vi;
							exprs[exprs_n] = vj  + "/" + vi + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
						}
						
						if (canPow) {
							
							arr1[0] = Math.pow(vi, vj);
							exprs[exprs_n] = vi  + "^" + vj + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
						
							arr1[0] = Math.pow(vj, vi);
							exprs[exprs_n] = vj  + "^" + vi + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
						}
						
						if (canFac) {
							/** 阶乘 */
							arr1[0] = fac(vi) + vj;
							exprs[exprs_n] = vi  + "!+" + vj + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = fac(vi) + fac(vj);
							exprs[exprs_n] = vi  + "!+" + vj + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = vi + fac(vj);
							exprs[exprs_n] = vi  + "+" + vj + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = fac(vi) - vj;
							exprs[exprs_n] = vi  + "!-" + vj + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = fac(vi) - fac(vj);
							exprs[exprs_n] = vi  + "!-" + vj + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
	
							arr1[0] = vi - fac(vj);
							exprs[exprs_n] = vi  + "-" + vj + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
	
							arr1[0] = fac(vj) - vi;
							exprs[exprs_n] = vj  + "!-" + vi + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = fac(vj) - fac(vi);
							exprs[exprs_n] = vj  + "!-" + vi + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = vj - fac(vi);
							exprs[exprs_n] = vj  + "-" + vi + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							
							arr1[0] = fac(vi) * vj;
							exprs[exprs_n] = vi  + "!*" + vj + "=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							arr1[0] = vi * fac(vj);
							exprs[exprs_n] = vi  + "*" + vj + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							arr1[0] = fac(vi) * fac(vj);
							exprs[exprs_n] = vi  + "!*" + vj + "!=" + arr1[0];
							if (search(arr1)) {
								return true;
							}
							

							if (vj !== 0) {
								arr1[0] = fac(vi) / vj;
								exprs[exprs_n] = vi  + "!/" + vj + "=" + arr1[0];
								if (search(arr1)) {
									return true;
								}
								arr1[0] = vi / fac(vj);
								exprs[exprs_n] = vi  + "/" + vj + "!=" + arr1[0];
								if (search(arr1)) {
									return true;
								}
								arr1[0] = fac(vi) / fac(vj);
								exprs[exprs_n] = vi  + "!/" + vj + "!=" + arr1[0];
								if (search(arr1)) {
									return true;
								}
							}
							
							if (vi !== 0) {
								arr1[0] = fac(vj) / vi;
								exprs[exprs_n] = vj  + "!/" + vi + "=" + arr1[0];
								if (search(arr1)) {
									return true;
								}
								arr1[0] = vj / fac(vi);
								exprs[exprs_n] = vj  + "/" + vi + "!=" + arr1[0];
								if (search(arr1)) {
									return true;
								}
								arr1[0] = fac(vj) / fac(vi);
								exprs[exprs_n] = vj  + "!/" + vi + "!=" + arr1[0];
								if (search(arr1)) {
									return true;
								}
							}
						}
						
						exprs.pop();
					}
				}
				return false;
			};
			
			if (!search(nums)) {
				canPow = true;
				exprs = [];
				if (!search(nums)) {
					canFac = true;
					exprs = [];
					if (!search(nums)) {
						document.write('无解');
					}
				};
			};
			
		</script>
	</body>
</html>